<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<link rel="stylesheet" href="../css/base.css">
<link rel="stylesheet" href="../css/form.css">

<body>
    <form action="">
        <span class="error">用户名密码错误</span>
        <label for="">
            用户名：<input name="username" type="text">
        </label>
        <label for="">
            密码：<input name="password" type="text">
        </label>
        <button>登录</button>
    </form>
    <script src="../../node_modules/jquery/dist/jquery.min.js"></script>
    <script>
        $('form').on('submit', async e => {
            e.preventDefault()
            console.log('发送请求');
            const res = await $.ajax({
                url: 'http://localhost:8080/users/login',
                method: 'post',
                data: $('form').serialize()
            })
            
            console.log(res);
            if (res.code !== 1) return $('.error').show()
            console.log(1);
            window.sessionStorage.setItem('token', res.token)
            window.sessionStorage.setItem('id', res.uesr_id)
            window.location.href = './index.html'
        })

    </script>
</body>

</html>